﻿using System;
using System.Globalization;
using System.IO;

namespace HslCommunication.LogNet
{
	/// <summary>
	/// 一个日志组件，可以根据时间来区分不同的文件存储<br />
	/// A log component that can distinguish different file storages based on time
	/// </summary>
	/// <remarks>
	/// 此日志实例将根据日期时间来进行分类，支持的时间分类如下：
	/// <list type="number">
	/// <item>分钟</item>
	/// <item>小时</item>
	/// <item>天</item>
	/// <item>周</item>
	/// <item>月份</item>
	/// <item>季度</item>
	/// <item>年份</item>
	/// </list>
	/// 当然你也可以指定允许存在多少个日志文件，比如我允许存在最多10个文件，如果你的日志是根据天来分文件的，那就是10天的数据。
	/// 同理，如果你的日志是根据年来分文件的，那就是10年的日志文件。
	/// </remarks>
	/// <example>
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\LogNet\LogNetSingle.cs" region="Example3" title="日期存储实例化" />
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\LogNet\LogNetSingle.cs" region="Example4" title="基本的使用" />
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\LogNet\LogNetSingle.cs" region="Example5" title="所有日志不存储" />
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\LogNet\LogNetSingle.cs" region="Example6" title="仅存储ERROR等级" />
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\LogNet\LogNetSingle.cs" region="Example7" title="不指定路径" />
	/// </example>
	// Token: 0x020000E0 RID: 224
	public class LogNetDateTime : LogPathBase, ILogNet, IDisposable
	{
		/// <summary>
		/// 实例化一个根据时间存储的日志组件，需要指定每个文件的存储时间范围<br />
		/// Instantiate a log component based on time, you need to specify the storage time range for each file
		/// </summary>
		/// <param name="filePath">文件存储的路径</param>
		/// <param name="generateMode">存储文件的间隔</param>
		/// <param name="fileQuantity">指定当前的日志文件数量上限，如果小于0，则不限制，文件一直增加，如果设置为10，就限制最多10个文件，会删除最近时间的10个文件之外的文件。</param>
		// Token: 0x06001139 RID: 4409 RVA: 0x0006253C File Offset: 0x0006073C
		public LogNetDateTime(string filePath, GenerateMode generateMode = GenerateMode.ByEveryYear, int fileQuantity = -1)
		{
			this.filePath = filePath;
			this.generateMode = generateMode;
			base.LogSaveMode = LogSaveMode.Time;
			this.controlFileQuantity = fileQuantity;
			bool flag = !string.IsNullOrEmpty(filePath) && !Directory.Exists(filePath);
			if (flag)
			{
				Directory.CreateDirectory(filePath);
			}
		}

		/// <inheritdoc />
		// Token: 0x0600113A RID: 4410 RVA: 0x00062594 File Offset: 0x00060794
		protected override string GetFileSaveName()
		{
			bool flag = string.IsNullOrEmpty(this.filePath);
			string result;
			if (flag)
			{
				result = string.Empty;
			}
			else
			{
				switch (this.generateMode)
				{
				case GenerateMode.ByEveryMinute:
					result = Path.Combine(this.filePath, "Logs_" + DateTime.Now.ToString("yyyyMMdd_HHmm") + ".txt");
					break;
				case GenerateMode.ByEveryHour:
					result = Path.Combine(this.filePath, "Logs_" + DateTime.Now.ToString("yyyyMMdd_HH") + ".txt");
					break;
				case GenerateMode.ByEveryDay:
					result = Path.Combine(this.filePath, "Logs_" + DateTime.Now.ToString("yyyyMMdd") + ".txt");
					break;
				case GenerateMode.ByEveryWeek:
				{
					GregorianCalendar gregorianCalendar = new GregorianCalendar();
					int weekOfYear = gregorianCalendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
					result = Path.Combine(this.filePath, string.Concat(new string[]
					{
						"Logs_",
						DateTime.Now.Year.ToString(),
						"_W",
						weekOfYear.ToString(),
						".txt"
					}));
					break;
				}
				case GenerateMode.ByEveryMonth:
					result = Path.Combine(this.filePath, "Logs_" + DateTime.Now.ToString("yyyy_MM") + ".txt");
					break;
				case GenerateMode.ByEverySeason:
					result = Path.Combine(this.filePath, string.Concat(new string[]
					{
						"Logs_",
						DateTime.Now.Year.ToString(),
						"_Q",
						(DateTime.Now.Month / 3 + 1).ToString(),
						".txt"
					}));
					break;
				case GenerateMode.ByEveryYear:
					result = Path.Combine(this.filePath, "Logs_" + DateTime.Now.Year.ToString() + ".txt");
					break;
				default:
					result = string.Empty;
					break;
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x0600113B RID: 4411 RVA: 0x000627D0 File Offset: 0x000609D0
		public override string ToString()
		{
			return string.Format("LogNetDateTime[{0}]", this.generateMode);
		}

		// Token: 0x0400047E RID: 1150
		private GenerateMode generateMode = GenerateMode.ByEveryYear;
	}
}
